public class FileSyncMenuActivity extends Activity {
    private static final int REQUEST_SYNC = 100;
    private static final int REQUEST_RECORD = 101;
    
    private final Handler mHandler = new Handler();
    private boolean mAttachedToWindow;
    private boolean mIsMenuClosed;
    private boolean mPreparePanelCalled;
    private boolean mFromLiveCardVoice;
    
    private FileSync mFileSync;
    private AudioRecorder mAudioRecorder;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (service instanceof FileSyncService.FileSyncBinder) {
                mFileSync = ((FileSyncService.FileSyncBinder) service).getFileSync();
                openMenu();
            }
            unbindService(this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // Nothing to do here
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 支持"ok glass"语音命令
        mFromLiveCardVoice = getIntent().getBooleanExtra(LiveCard.EXTRA_FROM_LIVECARD_VOICE, false);
        if (mFromLiveCardVoice) {
            getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
        }

        // 绑定同步服务
        Intent serviceIntent = new Intent(this, FileSyncService.class);
        bindService(serviceIntent, mConnection, 0);
        
        mAudioRecorder = new AudioRecorder(this);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mAttachedToWindow = true;
        openMenu();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mAttachedToWindow = false;
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        if (isMyMenu(featureId)) {
            getMenuInflater().inflate(R.menu.sync_menu, menu);
            return true;
        }
        return super.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onPreparePanel(int featureId, View view, Menu menu) {
        mPreparePanelCalled = true;
        if (isMyMenu(featureId)) {
            if (mFileSync == null) {
                return false;
            } else {
                // 设置菜单项状态
                setOptionsMenuState(menu.findItem(R.id.start_sync), !mFileSync.isSyncing());
                setOptionsMenuState(menu.findItem(R.id.start_recording), !mAudioRecorder.isRecording());
                setOptionsMenuState(menu.findItem(R.id.stop_recording), mAudioRecorder.isRecording());
                return !mIsMenuClosed;
            }
        }
        return super.onPreparePanel(featureId, view, menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (!isMyMenu(featureId)) {
            return super.onMenuItemSelected(featureId, item);
        }

        switch (item.getItemId()) {
            case R.id.start_sync:
                post(new Runnable() {
                    @Override
                    public void run() {
                        startSync();
                    }
                });
                return true;
                
            case R.id.start_recording:
                mAudioRecorder.startRecording();
                return true;
                
            case R.id.stop_recording:
                mAudioRecorder.stopRecording();
                return true;
                
            case R.id.stop:
                post(new Runnable() {
                    @Override
                    public void run() {
                        stopService(new Intent(FileSyncMenuActivity.this, FileSyncService.class));
                    }
                });
                return true;
                
            default:
                return super.onMenuItemSelected(featureId, item);
        }
    }

    @Override
    public void onPanelClosed(int featureId, Menu menu) {
        super.onPanelClosed(featureId, menu);
        if (isMyMenu(featureId)) {
            mIsMenuClosed = true;
            finish();
        }
    }

    private void openMenu() {
        if (mAttachedToWindow && mFileSync != null) {
            if (mFromLiveCardVoice) {
                if (mPreparePanelCalled) {
                    getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS);
                }
            } else {
                openOptionsMenu();
            }
        }
    }

    private boolean isMyMenu(int featureId) {
        return featureId == Window.FEATURE_OPTIONS_PANEL ||
               featureId == WindowUtils.FEATURE_VOICE_COMMANDS;
    }

    private static void setOptionsMenuState(MenuItem menuItem, boolean enabled) {
        menuItem.setVisible(enabled);
        menuItem.setEnabled(enabled);
    }

    private void startSync() {
        mFileSync.startSync();
    }

    protected void post(Runnable runnable) {
        mHandler.post(runnable);
    }
} 